<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
    :immediate-check="false"
  >
    <CommentItem
      @replyHandler="$emit('replyHandler', $event)"
      v-for="item in list"
      :key="item.com_id"
      :comment="item"
    ></CommentItem>
    <!-- <van-cell
      v-for="item in list"
      :key="item.com_id"
      :title="item.content"
    ></van-cell> -->
  </van-list>
</template>
<script>
import { getCommentLIstApi } from "@/api/article";
import CommentItem from "./comment-item.vue";
export default {
  name: "CommentList",
  components: {},
  props: {
    sourceId: {
      type: [String, Number],
      required: true,
    },
    list: {
      type: Array,
      required: true,
      default: () => [],
    },
    type: {
      type: String,
      default: "a",
    },
  },
  components: {
    CommentItem,
  },
  data() {
    return {
      //   list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      pageSize: 10,
      offset: null,
    };
  },
  created() {
    this.loading = true;
    this.onLoad();
  },

  methods: {
    async onLoad() {
      try {
        //1.请求获取数据
        let { data } = await getCommentLIstApi({
          type: this.type, //评论类型
          source: this.sourceId, //文章id或者评论id
          offset: this.offset, //评论数据的偏移量,获取下一页数据的标记--last_id
          limit: this.pageSize, //获取评论个数
        });
        //2.将数据追加给列表
        console.log(data);
        this.list.push(...data.data.results);
        //3.关闭loading
        this.loading = false;
        //4.判断是否结束,是否还有数据
        if (data.data.results.length < this.pageSize) {
          this.finished = true;
        } else {
          this.offset = data.data.last_id;
        }
        //将品论或者恢复总数量恢复到外面去
        this.$emit("sendTotal", data.data.total_count);
        //5.判断是否出错
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>
